-- #####################################################################################################################
-- 定义变量;
-- 第一个表示插件名,第二个是一个table,这个table可以作为各个文件间交换数据的存储位置
-- wow加载这个文件时,会吧这2个全局变量传进来
local addonName, addon = ...
-- #####################################################################################################################
local frameName = "UI-Frame";
local subFrame = CreateFrame("Frame", frameName, nil, InterfaceOptionsFramePanelContainer, nil)
subFrame:Hide();
-- #####################################################################################################################
subFrame:RegisterEvent("ADDON_LOADED")
subFrame:SetScript("OnEvent", function(self, event, loadedAddon)
    if event == "ADDON_LOADED" and loadedAddon == addonName then
        self:UnregisterEvent("ADDON_LOADED")
        addon:InterfaceOptions_AddApiSubCategory(subFrame, subFrame:GetName())
    end
end)
-- #####################################################################################################################
local miniMapButton = nil
local dayTaskFrame = nil
subFrame:SetScript("OnShow", function(frame)
    --------------------------------------------------------------------------------------------------------------------------
    local title = subFrame:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", 16, -16)
    title:SetText(frameName)
    --------------------------------------------------------------------------------------------------------------------------
    local testMiniMapButton = CreateFrame("Button", frameName .. "_ReloadButton", frame, "UIPanelButtonTemplate")
    testMiniMapButton:SetText("测试小地图按钮")
    testMiniMapButton:SetSize(180, 25)
    testMiniMapButton:SetEnabled(true)
    testMiniMapButton:SetPoint("TOPLEFT", title, "BOTTOMLEFT", -2, -16)
    testMiniMapButton:SetScript("OnClick", function()
        if miniMapButton == nil then
            -- 小地图按钮
            miniMapButton = CreateFrame("Button", "MiniMapButton", Minimap)
            miniMapButton:SetWidth(32)
            miniMapButton:SetHeight(32)
            miniMapButton:SetFrameStrata("LOW") -- MEDIUM/LOW
            miniMapButton:SetFixedFrameStrata(true)
            miniMapButton:SetFrameLevel(8)
            miniMapButton:SetFixedFrameLevel(true)
            miniMapButton:Raise()
            ---？
            miniMapButton:SetNormalTexture("Interface\\BUTTONS\\UI-Quickslot-Depress")
            miniMapButton:SetHighlightTexture("Interface\\buttons\\iconborder-glowring")
            miniMapButton:SetPoint("TOPLEFT", Minimap, "BOTTOMLEFT", 0, 0)
            --miniMapButton:SetPoint("CENTER", Minimap, "CENTER", 0, 0)
            --miniMapButton:SetPoint("CENTER", Minimap, "CENTER", math.cos(math.rad(225)), math.sin(math.rad(225)))

            -- 创建小地图上的图标
            local icon = miniMapButton:CreateTexture(nil, "BACKGROUND")
            icon:SetWidth(32)
            icon:SetHeight(32)
            icon:SetPoint("CENTER", miniMapButton, "CENTER", 0, 0)
            icon:SetTexture("Interface\\Icons\\INV_Misc_Coin_08")
            miniMapButton.icon = icon

            -- 按钮上的提示
            miniMapButton:SetScript("OnEnter", function(self)
                GameTooltip:SetOwner(self, "ANCHOR_LEFT")
                GameTooltip:AddLine("|cff1eff00每日任务:|r 隐藏/显示")
                GameTooltip:Show()
            end)
            miniMapButton:SetScript("OnLeave", function()
                GameTooltip:Hide()
            end)
            -- 按钮点击
            miniMapButton:SetScript("OnClick", function()
                print("我被点击了")
                if not dayTaskFrame then
                    dayTaskFrame = CreateFrame("Frame", "myGUI", UIParent, "BackdropTemplate")
                    dayTaskFrame:SetWidth(200) --
                    dayTaskFrame:SetHeight(300) --
                    dayTaskFrame:SetFrameLevel(1) --
                    dayTaskFrame:Show()

                    --dayTaskFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", xoffset, yoffset)
                    --dayTaskFrame:SetPoint("CENTER", 0, 0) -- CENTER BOTTOM TOP LEFT RIGHT BOTTOMLEFT BOTTOMRIGHT TOPLEFT TOPRIGHT
                    dayTaskFrame:SetPoint("TOPLEFT", 20, -140) --

                    dayTaskFrame:SetFrameStrata("DIALOG") -- DIALOG|HIGH|LOW
                    dayTaskFrame:SetBackdrop({
                        bgFile = "Interface/Tooltips/ChatBubble-Background",
                        edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
                        tile = false, --
                        tileEdge = false, --
                        tileSize = 0, --
                        edgeSize = 1, --
                        insets = { left = 1, right = 1, top = 1, bottom = 1 }, --
                    })
                    dayTaskFrame:SetBackdropBorderColor(0, 0, 0, 1) --

                    -- 默认情况下是显示的状态
                    --f:Hide()
                    -- 显示
                    --f:Show()
                    -- self:CreateFontString(name, drawLayer, templateName)
                    local title = dayTaskFrame:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
                    title:SetPoint("CENTER", 0, 130)
                    title:SetText("每日任务列表")
                    -- FIXME(ljs):如何设置字体大小？

                    --dayTaskFrame:Raise()
                    dayTaskFrame:EnableMouse(true)
                    dayTaskFrame:SetMovable(true)
                    dayTaskFrame:RegisterForDrag("LeftButton")
                    dayTaskFrame:SetScript("OnDragStart", dayTaskFrame.StartMoving)
                    dayTaskFrame:SetScript("OnDragStop", dayTaskFrame.StopMovingOrSizing)

                    local border = CreateFrame('Frame', nil, dayTaskFrame, 'DialogBorderTemplate')
                    border:SetAlpha(0.5)
                    local header = CreateFrame('Frame', nil, dayTaskFrame, 'DialogHeaderTemplate')
                    header:SetAlpha(0.7)
                    local closeButton = CreateFrame('Button', nil, dayTaskFrame, 'UIPanelCloseButton')
                    closeButton:SetPoint('TOPRIGHT')
                else
                    if dayTaskFrame:IsShown() then
                        dayTaskFrame:Hide()
                    else
                        dayTaskFrame:Show()
                    end
                end
            end)
        else
            if miniMapButton:IsShown() then
                miniMapButton:Hide()
            else
                miniMapButton:Show()
            end
        end
    end)
    --------------------------------------------------------------------------------------------------------------------------
    local fontSizeDropdown = CreateFrame("Frame", "BugSackFontSize", frame, "UIDropDownMenuTemplate")
    fontSizeDropdown:SetPoint("TOPLEFT", testMiniMapButton, "BOTTOMLEFT", -15, -10)
    --------------------------------------------------------------------------------------------------------------------------
    local testTemplateOfUIPanelButtonTemplate = CreateFrame("Button", frameName .. "_ReloadButton", frame, "UIPanelButtonTemplate")
    testTemplateOfUIPanelButtonTemplate:SetText("测试Frame模板-1-UIPanelButtonTemplate")
    testTemplateOfUIPanelButtonTemplate:SetSize(360, 25)
    testTemplateOfUIPanelButtonTemplate:SetEnabled(true)
    testTemplateOfUIPanelButtonTemplate:SetPoint("TOPLEFT", fontSizeDropdown, "BOTTOMLEFT", -2, -16)
    testTemplateOfUIPanelButtonTemplate:SetScript("OnClick", function(self, button, down)
        addon:CreateMyFrame("Button", "UIPanelButtonTemplate")
        --addon:CreateMyFrame("Button", "UIPanelButtonTemplate2")
        --addon:CreateMyFrame("Frame", "UIPanelCloseButton")
    end)
    --------------------------------------------------------------------------------------------------------------------------
end)
-- #####################################################################################################################
-- 自定义宏命令
-- SLASH_<CommandId><num>=<CommandText> # num=数字编码
-- SlashCmdList["<CommandId>"] = <CodeToExecute> # CodeToExecute=执行的代码
SLASH_MyCmd1 = '/rl'
SLASH_MyCmd2 = '/rl2'
SLASH_MyTest1 = '/test'
-- 定义宏命令对应的事件
SlashCmdList["MyCmd"] = function()
    ReloadUI()
end
SlashCmdList["MyTest"] = function()
    if frame:IsShown() then
        frame:Hide()
        print("窗口消失")
    else
        frame:Show()
        print("窗口出现")
    end
end
-- #####################################################################################################################
-- frameType:Frame|Button
-- frameTemplate:
---------------- UIPanelButtonTemplate  :
---------------- UIPanelButtonTemplate2
---------------- UIPanelCloseButton
---------------- InputBoxTemplate
---------------- UICheckButtonTemplate
---------------- TabButtonTemplate
---------------- GameMenuButtonTemplate
---------------- UIRadioButtonTemplate
---------------- OptionsSliderTemplate
function addon:CreateMyFrame(frameType, frameTemplate)
    -- frame = CreateFrame("frameType" [, "name" [, parent [, "template"]]])
    local frame = CreateFrame(frameType, nil, UIParent, frameTemplate)
    frame:SetWidth(400)
    frame:SetHeight(400)
    frame:SetPoint("TOPLEFT", 20, -140)
    print("frame:GetFrameLevel() = ", frame:GetFrameLevel())
    return frame
end
-- #####################################################################################################################